// ARFrameGrabber.h: interface for the ARFrameGrabber class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_ARFRAMEGRABBER_H__C5553937_4BAB_4FEF_B4A6_1693AB0C99E3__INCLUDED_)
#define AFX_ARFRAMEGRABBER_H__C5553937_4BAB_4FEF_B4A6_1693AB0C99E3__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

//#include <AR/config.h

#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <dshow.h>
//#include <streams.h>
#include <atlbase.h>

#define __IDxtCompositor_INTERFACE_DEFINED__ 
#define __IDxtAlphaSetter_INTERFACE_DEFINED__ 
#define __IDxtJpeg_INTERFACE_DEFINED__ 
#define __IDxtKey_INTERFACE_DEFINED__ 
#include <qedit.h>

class SmartString
{
public:
	SmartString():str(NULL) {}

	SmartString(char* pStr):str(NULL)
	{
		if (pStr)
		{
			int size = int(strlen(pStr));
			str = new char[size+1];
			strcpy(str, pStr);
		}
	}

	SmartString(SmartString& sStr)
	{
		SetString(sStr.GetBuffer());
	}

	~SmartString()
	{
		if (str)
			delete[] str;
	}

	SmartString& operator =(char* pStr)
	{
		SetString(pStr);
		return *this;
	}

	SmartString& operator =(SmartString& sStr)
	{
		SetString(sStr.GetBuffer());
		return *this;
	}

	char* GetBuffer() {return str;}

protected:
	void SetString(char *pStr)
	{
		if (str)
			delete[] str;

		if (!pStr)
		{
			str = NULL;
		}
		else
		{
			int size = int(strlen(pStr));
			str = new char[size + 1];
			strcpy(str, pStr);
		}

	}


	char* str;
};

class DeviceInfo
{
public:
	DeviceInfo():next(NULL), deviceId(-1)
	{
	} 
	~DeviceInfo()
	{
		if (next)
			delete next;
	}


	SmartString friendlyName;
	int			deviceId;	
	DeviceInfo* next;
};


class  ARFrameGrabber  
{
public:
	ARFrameGrabber();
	virtual ~ARFrameGrabber();

	void Init(int deviceId, bool displayProperties = true);
	void BindFilter(int deviceId, IBaseFilter **pFilter);
	void GrabFrame(long* size, long** pBuffer);
	void GrabFrame();
	void Grab32BitFrame();
	void GrabByteFrame();
	void SetCrossBar();
	


	long  GetBufferSize() {return bufferSize;}
	long* GetBuffer() {return pBuffer;}
	BYTE* GetByteBuffer() {return pBYTEbuffer;}
	
	void SetFlippedImage(bool flag) {flipImage = flag;}

	void DisplayProperties();
	void EnumDevices(DeviceInfo *head);
    int GetWidth();
    int GetHeight();

protected:
	CComPtr<IGraphBuilder> pGraph;
	CComPtr<IBaseFilter> pDeviceFilter;
	CComPtr<IMediaControl> pMediaControl;
	CComPtr<IBaseFilter> pSampleGrabberFilter;
	CComPtr<ISampleGrabber> pSampleGrabber;
	CComPtr<IPin> pGrabberInput;
	CComPtr<IPin> pGrabberOutput;
	CComPtr<IPin> pCameraOutput;
	CComPtr<IMediaEvent> pMediaEvent;
	CComPtr<IBaseFilter> pNullFilter;
	CComPtr<IPin> pNullInputPin;

	void FlipImage(long* pBuf);

private:
	void ReportError(char *msg);

	bool flipImage;
	long bufferSize;
	long *pBuffer;
	BYTE *pBYTEbuffer;
    bool connected;
    int width;
    int height;
};

#endif // !defined(AFX_ARFRAMEGRABBER_H__C5553937_4BAB_4FEF_B4A6_1693AB0C99E3__INCLUDED_)
